<template>
  <section class="todoapp">
    <!-- 除了驼峰, 还可以使用-转换链接 -->
    <TodoHeader :list="showList" @setMssage="setMssage" ></TodoHeader>
    <TodoMain :list="showList" @removeFn="removeFn"> </TodoMain>
    <TodoFooter :list="showList" @toggle="toggle" @clear="clear"></TodoFooter>
  </section>
</template>

<script>
// 1.0 样式引入
import "../styles/base.css"
import "../styles/index.css"
    
import TodoHeader from "./components/todoHeader";
import TodoMain from "./components/todoMain";
import TodoFooter from "./components/todoFooter";


export default {
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
  data(){
    return {
      list:JSON.parse(localStorage.getItem('todo')) 
      // list: [
      //   { id: 100, name: "吃饭", isDone: true },
      //   { id: 102, name: "睡觉", isDone: false },
      //   { id: 103, name: "打豆豆", isDone: true },
      // ]
      ,
      flag:'',
      optValue:''
    }
  },
  methods:{
    // 增加
    setMssage(val){
      this.list.push({
        id:this.list.length?this.list[this.list.length-1].id+1:100,
        name:val,
        isDone:false
      })
    },
    // 删除单个
    removeFn(index){
      this.list.splice(index,1)
    },
    //显示切换步骤1
    toggle(val){
      this.flag=val
    },
    //清除已完成
    clear(){
      this.list=this.list.filter(item=>item.isDone==false)
    },
  },
  computed:{
    //显示切换步骤2
    showList(){
      if(this.flag==="no"){
        return this.list.filter(item=>item.isDone==false)
      }else if(this.flag==="yes"){
        return this.list.filter(item=>item.isDone==true)
      }else{
        return this.list
      }
    }
  },
  watch:{
    list:{
      deep:true,
      handler(newVal,oldVal){
        localStorage.setItem('todo',JSON.stringify(newVal))
      }
    }
  }
};
</script>